# -*- coding: utf-8 -*-
import config
import os

def set_string_xml():
    string_xml_data = open('strings.xml').read()
    f = open(os.path.join('..', 'res', 'values', 'strings.xml'), 'w')
    f.write(string_xml_data.replace('APP_NAME', config.APP_NAME))
    f.close()

def set_manifest_xml():
    manifest_xml_data = open('AndroidManifest.xml').read() 
    f = open(os.path.join('..','AndroidManifest.xml'), 'w')
    manifest_xml_data = manifest_xml_data.replace('PACKAGE_NAME', config.PACKAGE_NAME)
    f.write(manifest_xml_data)
    f.close()

def getFilepath():
    filepath = config.PACKAGE_NAME.split('.')
    filepath.insert(0, 'src') 
    filepath.insert(0, '..') 
#    print filepath
    return filepath
 
def set_startup():
    if not os.path.isdir(os.path.sep.join(getFilepath())):
        os.mkdir(os.path.sep.join(getFilepath()))
    startup_path = getFilepath() 
    startup_path.append('StartupAct.java')
    f = open(os.path.sep.join(startup_path),'w')
    data = open('StartupAct.java').read()
    f.write(data.replace('APP_ID', config.APP_ID))
    f.close()

def set_ARMed_Startup():
    if not os.path.isdir(os.path.sep.join(getFilepath())):
        os.mkdir(os.path.sep.join(getFilepath()))
    filepath = getFilepath()
    filepath.append('StartupAct.java')
    f = open(os.path.sep.join(filepath),'w')
    data = open('ArmStartupAct.java').read()
    data = data.replace('APP_AID', config.APP_AID)
    data = data.replace('APP_ID', config.APP_ID)
    f.write(data)
    f.close()




def set_dataservice():
    if not os.path.isdir(os.path.sep.join(getFilepath())):
        os.mkdir(os.path.sep.join(getFilepath()))
    dataup_path = getFilepath()
    dataup_path.append('dataUpdateService.java')
    f = open(os.path.sep.join(dataup_path),'w')
    data = open('dataUpdateService.java').read()
    f.write(data.replace('APP_ID', config.APP_ID))
    f.close()

def set_movie_exist():
    #have_movie = config.HAVE_MOVIE
    data = ''
    if config.HAVE_MOVIE:
        data = '''
Tab tab4 = null;
intent = new Intent(context, MovieAct.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
tab4 = new Tab(context, "MOVIE");
tab4.setIcon(R.drawable.tabbar_tab4);
tab4.setIconSelected(R.drawable.tabbar_tab4_selected);
tab4.setIntent(intent);
tabHost.addTab(tab4);
'''
    file_data = open('BottomTabHostProvider.java').read()
    f = open(os.path.sep.join(['..','src','hw','ats','news','ui','BottomTabHostProvider.java']), 'w')
    f.write(file_data.replace('HAVE_MOVIE', data))
    f.close()


def main():
    set_movie_exist() 
    set_string_xml()
    set_manifest_xml()
    if config.isDRM :
        set_ARMed_Startup()
    else :
        set_startup()
#    set_dataservice()

main()

